<html lang="zh">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>弹幕转ass</title>
    </head>
    <body>
        <div class="content">
            <div>
                <span>以ass格式下载bilibili弹幕，兼容libass和vsfilter的ass格式解析器。</span>
            </div>
            <div>
                <input type="file" ref="file" @change="onFileChange">
            </div>
        </div>
        <script src="vue.js"></script>
        <script src="bliass.js"></script>
        <script>
        (function(){
            Vue.prototype.$sleep = function(ms) {
                ms = ms || 3000;
                return new Promise(function(resolve){
                    setTimeout(resolve, ms);
                });
            };
            Vue.prototype.$confirm = function(title, content) {
                let self = this;
                return new Promise(function(resolve, reject){
                    self.$Modal.confirm({
                        title: title,
                        content: content,
                        onOk: resolve,
                        onCancel: reject
                    });
                });
            };
        })();
    </script>
        <script>
            new Vue({
                el: '#page',
                methods: {
                    async onFileChange(e) {
                        let file = this.$refs.file.files[0];
                        await this.convertAss(file);
                    },
                    async convertAss(file){
                        if (file.size > (1 << 24)) {
                            throw new Error('不支持超过16M的文件');
                        }
                        console.log('开始处理');
                        let b2s = new Bilibili2Ass();
                        await b2s.loadFile(file);
                        let ass = b2s.toAss();
                        console.log('ass', ass);
                        this.download('\ufeff' + ass, file.name.replace(/\.[^.]*$/, '') + '.ass')
                    },
                    async download(data, filename) {
                        let blob = new Blob([data], { type: 'application/octet-steam' });
                        let url = URL.createObjectURL(blob);
                        let saveas = document.createElement('a');
                        saveas.href = url;
                        saveas.style.dispaly = 'none';
                        document.body.appendChild(saveas);
                        saveas.download = filename;
                        saveas.click();
                        await this.$sleep(1000);
                        document.addEventListener('unload', () => {
                            URL.revokeObjectURL(url);
                        });
                    }
                }
            })
        </script>
    </body>
</html>
